package com.chenglai.goods.controller;

import com.chenglai.commonutils.common.Result;
import com.chenglai.commonutils.common.StatusCode;
import com.chenglai.goods.pojo.Goods;
import com.chenglai.goods.service.GoodsService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author yawang
 * @create 2023/11/14 14:52:26
 **/
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @PostMapping("/listGoods")
    @ApiOperation(value = "获取商品列表")
    public Result listGoods(@ApiParam(name="shopId",value = "门店ID") Integer shopId,
                            @ApiParam(name="page",value = "页码") Integer page,
                            @ApiParam(name="size",value = "每页条数") Integer size,
                            @ApiParam(name="categoryId",value = "分类ID") Integer categoryId,
                            @ApiParam(name="flag",value = "0为全部 1为销售中 2为下架中，3为售罄") Integer flag,
                            @ApiParam(name="startTime",value = "开始时间") String  startTime,
                            @ApiParam(name="endTime",value = "结束时间") String  endTime,
                            @ApiParam(name="search",value = "搜索内容") String  search
                         ){

        try {
            PageInfo map = goodsService.listGoods(shopId,page,size,categoryId,flag,startTime,endTime,search);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, StatusCode.OK,"查询失败");
        }
    }
    @PostMapping("/listGoods")
    @ApiOperation(value = "添加/修改商品参数")
    public Result addGoods(Goods goods,
                           @ApiParam(name="flag",value = "add/edit,注意，商品总库查询需要把sku的库粗相加传过来")String flag){
        try {
            goodsService.addGoods(goods,flag);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, StatusCode.OK,"添加失败");
        }
    }

    @PostMapping("/listGroupGoods")
    @ApiOperation(value = "获取团购商品列表")
    public Result listGroupGoods(@ApiParam(name="shopId",value = "门店ID") Integer shopId){
        try {
            List<Goods> groupGoodsList = goodsService.listGroupGoods(shopId);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, StatusCode.OK,"添加失败");
        }
    }



}
